package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.dao.CommentMapper;
import com.itheima.dao.FeedbackMapper;
import com.itheima.pojo.*;
import com.itheima.service.CommentReportService;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/web/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private CommentReportService commentReportService;

    /**
     * 增加评论
     * @param commentVO
     * @return
     */
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody CommentVO commentVO){
        commentService.add(commentVO);
        //封装返回对象
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data","插入成功");
        return result;
    }

    /**
     * 关闭评论邮件通知
     * @param userUid
     * @return
     */
    @GetMapping("/closeEmailNotification/{userUid}")
    public Map<String,Object> closeEmailNotification(@PathVariable("userUid") String userUid){
        commentMapper.closeEmailNotification(userUid);
        //封装返回对象
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data","操作成功");
        return result;
    }

    /**
     * 删除评论
     * @param commentVO
     * @return
     */
    @PostMapping("/delete")
    public Map<String,Object> delete(@RequestBody CommentVO commentVO){
        commentService.delete(commentVO);
        //封装返回对象
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data","删除成功");
        return result;
    }

    /**
     * 获取评论列表
     * @param commentVO
     * @return
     */
    @PostMapping("/getLIst")
    public Map<String,Object> getLIst(@RequestBody CommentVO commentVO){

        PageInfo<Comment> pageInfo = commentService.getList(commentVO);

        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",data);
        return result;
    }

    /**
     * 获取评论列表和回复
     * @param userVO
     * @return
     */
    @PostMapping("/getLIstByUser")
    public Map<String,Object> getLIstByUser(@RequestBody UserVO userVO){
        String userUid = userVO.getUid();
        //根据userUid获取评论列表
        List<Comment> commentList = commentMapper.getCommentListByUserUid(userUid);
        //根据userUid获取回复列表
        List<Feedback> feedbackList = feedbackMapper.getFeedbackListByUserUid(userUid);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","SUCCESS");
        Map<String,Object> data=new HashMap<>();
        data.put("commentList",commentList);
        data.put("feedbackList",feedbackList);
        result.put("data",data);

        return result;
    }

    /**
     * 获取用户点赞信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @PostMapping("/getPraiseListByUser")
    public Map<String,Object> getPraiseListByUser(@RequestParam(name = "currentPage",defaultValue = "1",required = false) Integer currentPage,@RequestParam(name = "pageSize",defaultValue = "10",required = false) Integer pageSize){
        PageInfo<Comment> pageInfo = commentService.getPraiseListByUser(currentPage, pageSize);
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("records",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",new ArrayList<>());
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        result.put("data",data);
        return result;
    }

    /**
     * 举报评论
     * @param commentVO
     * @return
     */
    @PostMapping("/report")
    public Map<String,Object> report(@RequestBody CommentVO commentVO){
        CommentReport commentReport=new CommentReport();
        //获取被举报的评论对象
        Comment comment = commentMapper.getByUid(commentVO.getUid());
        //封装举报对象
        commentReport.setUid(UUID.randomUUID().toString());
        commentReport.setProgress(0);
        commentReport.setContent(commentVO.getContent());
        commentReport.setCreateTime(new Date());
        commentReport.setReportCommentUid(commentVO.getToUserUid());
        commentReport.setReportUserUid(commentVO.getUserUid());
        commentReport.setStatus(0);
        commentReport.setUpdateTime(new Date());
        commentReport.setUserUid(commentVO.getUserUid());
        commentReportService.add(commentReport);
        //封装返回对象
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data","举报成功");
        return result;
    }
}
